package com.dp.appi.dao.impl;

import com.dp.appi.dao.ExperienceDao;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/5/3.
 */
@Component
public class ExperienceDaoImpl extends BaseDaoImpl implements ExperienceDao {
    /**
     * 我的经验
     *
     * @param name 查询关键字
     * @param s    开始序列
     * @param n    获取条数
     * @param id   用户id
     * @return
     */
    @Override
    public List<Map<String, Object>> experienceList(String name, Integer s, Integer n, String id) {
        String sql="select ee.experience_id,ee.article_thumb,ee.title,ee.intro,ee.article_thumb,tsbu.realname as auditor," +
                " UNIX_TIMESTAMP(ee.audit_time) audit_time,ee.audit_status,ee.remark,UNIX_TIMESTAMP(ee.create_time) create_time" +
                " from e_experience ee" +
                " LEFT JOIN t_s_base_user tsbu on ee.user_id=tsbu.ID" +
                " where ee.user_id=? and ee.deleted=0" ;
        if(!StringUtils.isEmpty(name)){
            sql+=" AND ee.title like '%"+name+"%'";
        }
        sql+=" ORDER BY ee.create_time desc limit ?,?";
        return getJdbcTemplate().queryForList(sql,new Object[]{id,s,n});
    }

    /**
     * 删除我的经验
     *
     * @param experience_id 经验id
     * @param id            用户id
     */
    @Override
    public void delExp(Long experience_id, String id) {
        String sql="update e_experience set deleted=1  where experience_id=? and user_id=?";
        getJdbcTemplate().update(sql,new Object[]{experience_id,id});
    }

    /**
     * 发布经验
     *
     * @param experience_id 经验id
     * @param id            用户id
     */
    @Override
    public void publishExp(Long experience_id, String id) {
        String sql="update e_experience set audit_status=1002 where experience_id=? and user_id=?";
        getJdbcTemplate().update(sql,new Object[]{experience_id,id});
    }

    /**
     * 获取经验内容
     *
     * @param experience_id 经验id
     */
    @Override
    public Map<String, Object> getExperienceContent(Long experience_id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select `experience_id`,  `title`, `content`, `intro`, `article_thumb`, ");
        sql.append("UNIX_TIMESTAMP(`audit_time`) audit_time,  `audit_status`, ");
        sql.append("`remark`, UNIX_TIMESTAMP(`create_time`) create_time ");
        sql.append("from e_experience a ");
        sql.append("where deleted = 0 and a.experience_id = ? ");
        Map<String,Object> map = findOneObject(sql.toString(),new Object[]{experience_id});
        if(map!=null){
            String sql2 = "select attachment_id,name,postfix,path from e_exp_attachment where experience_id=?";
            List<Map<String,Object>> list = getJdbcTemplate().queryForList(sql2,new Object[]{experience_id});
            map.put("attachment",list);
            return map;
        }else{
            return new HashMap<>();
        }
    }
}
